home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / libcs / intarg.c < prev    next >
C/C++ Source or Header  |  1995-06-12  |  3KB  |  94 lines

  1. /*
  2.  * Copyright (c) 1990 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS" AND CARNEGIE MELLON UNIVERSITY
  12.  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT
  14.  * SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL, DIRECT,
  15.  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  17.  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * Users of this software agree to return to Carnegie Mellon any
  21.  * improvements or extensions that they make and grant Carnegie the
  22.  * rights to redistribute these changes.
  23.  *
  24.  * Export of this software is permitted only after complying with the
  25.  * regulations of the U.S. Deptartment of Commerce relating to the
  26.  * Export of Technical Data.
  27.  */
  28. /*  intarg  --  parse integer argument
  29.  *
  30.  *  Usage:  i = intarg (ptr,brk,prompt,min,max,default)
  31.  *    int i,min,max,default;
  32.  *    char **ptr,*brk,*prompt;
  33.  *
  34.  *  Will attempt to parse an argument from the string pointed to
  35.  *  by "ptr", incrementing ptr to point to the next arg.  If
  36.  *  an arg is found, it is converted into an integer.  If there is
  37.  *  no arg or the value of the arg is not within the range
  38.  *  [min..max], then "getint" is called to ask the user for an
  39.  *  integer value.
  40.  *  "Brk" is the list of characters which may terminate an argument;
  41.  *  if 0, then " " is used.
  42.  *
  43.  *  HISTORY
  44.  * $Log:    intarg.c,v $
  45.  * Revision 1.2  90/12/11  17:56:28  mja
  46.  *     Add copyright/disclaimer for distribution.
  47.  * 
  48.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  49.  *    Modified for 4.2 BSD.  Now puts output on stderr.
  50.  *
  51.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  52.  *    Rewritten for VAX.
  53.  *
  54.  */
  55.  
  56. #include <ctype.h>
  57. #include <stdio.h>
  58.  
  59. int getint();
  60. char *nxtarg();
  61. long atol();
  62. int strcmp();
  63.  
  64. int intarg (ptr,brk,prompt,min,max,defalt)
  65. char **ptr;
  66. char *brk,*prompt;
  67. int min,max,defalt;
  68. {
  69.     register int i;
  70.     register char *arg,*p;
  71.  
  72.     arg = nxtarg (ptr,brk);
  73.     fflush (stdout);
  74.  
  75.     if (*arg != '\0') {        /* if there was an arg */
  76.         for (p=arg; *p && (isdigit(*p) || *p == '-' || *p == '+'); p++) ;
  77.         if (*p) {
  78.             if (strcmp(arg,"?") != 0)  fprintf (stderr,"%s not numeric.  ",arg);
  79.         } 
  80.         else {
  81.             i = atol (arg);
  82.             if (i<min || i>max) {
  83.                 fprintf (stderr,"%d out of range.  ",i);
  84.             }
  85.         }
  86.     }
  87.  
  88.     if (*arg == '\0' || *p != '\0' || i<min || i>max) {
  89.         i = getint (prompt,min,max,defalt);
  90.     }
  91.  
  92.     return (i);
  93. }
  94.